package com.ys.peony.ui.adapter;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ys.peony.R;
import com.ys.peony.model.VehicleModel;
import com.ys.peony.model.YWDJPrepareItemModel;
import com.ys.peony.ui.view.zlistview.adapter.BaseSwipeAdapter;
import com.ys.peony.ui.view.zlistview.enums.DragEdge;
import com.ys.peony.ui.view.zlistview.enums.ShowMode;
import com.ys.peony.ui.view.zlistview.listener.SimpleSwipeListener;
import com.ys.peony.ui.view.zlistview.widget.ZSwipeItem;
import com.ys.peony.utils.CommonUtils;
import com.ys.peony.utils.DateUtils;
import com.ys.peony.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class DYWListViewAdapter extends BaseSwipeAdapter {
    protected static final String TAG = DYWListViewAdapter.class.getSimpleName();
    private Activity context;
    private List<VehicleModel> data;
    private List<YWDJPrepareItemModel> ownerShopList;

    public DYWListViewAdapter(Activity context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public VehicleModel getItem(int position) {
        return data == null ? null : data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.swipe_item;
    }

    @Override
    public View generateView(int position, ViewGroup parent) {
        return context.getLayoutInflater().inflate(R.layout.dyw_item_layout,
                parent, false);
    }

    @Override
    public void fillValues(final int position, final View convertView) {
        final ZSwipeItem swipeItem = (ZSwipeItem) convertView
                .findViewById(R.id.swipe_item);
        LinearLayout rightLL = (LinearLayout) convertView.findViewById(R.id.right_ll);
        TextView delBtn = (TextView) convertView.findViewById(R.id.item_delete_tv);

        TextView dywTitleTv = (TextView) convertView.findViewById(R.id.dyw_title_tv);
        TextView branchTV = (TextView) convertView.findViewById(R.id.branch_tv);
        TextView modelTV = (TextView) convertView.findViewById(R.id.model_tv);
//        TextView idCardTV = (TextView) convertView.findViewById(R.id.idcard_tv);
        TextView carNumTV = (TextView) convertView.findViewById(R.id.number_tv);
//        TextView driverNumTV = (TextView) convertView.findViewById(R.id.drivernum_tv);
        TextView buyTimeTV = (TextView) convertView.findViewById(R.id.buytime_tv);
        TextView distanceTv = (TextView) convertView.findViewById(R.id.distance_tv);
        TextView configTv = (TextView) convertView.findViewById(R.id.configure_tv);
        TextView ownerShipTagTv = (TextView) convertView.findViewById(R.id.ownership_tag_tv);
        TextView ownerShipPersonTv = (TextView) convertView.findViewById(R.id.ownership_person_tv);

        final VehicleModel item = data.get(position);
        if (item == null) {
            return;
        }
        dywTitleTv.setText("抵押物"+position);
        branchTV.setText(StringUtils.trimNull(item.getBrand_name()));
        modelTV.setText(StringUtils.trimNull(item.getVehicle_type()));
//        idCardTV.setText(StringUtils.trimNull(item.getVehicle_id()));
        carNumTV.setText(StringUtils.trimNull(item.getPlate_number()));
//        driverNumTV.setText(StringUtils.trimNull(item.getEngine_number()));
        buyTimeTV.setText(DateUtils.formatTimeToStr(item.getTime_buying().getTime(),DateUtils.formatDate));
        distanceTv.setText(item.getKilometer()+"");

        configTv.setText(StringUtils.trimNull(item.getVehicle_grade()));
        ownerShipTagTv.setText(CommonUtils.getTextbyVal(item.getOwnership_tag(),ownerShopList));
        ownerShipPersonTv.setText(StringUtils.trimNull(item.getOwnership_person()));

        swipeItem.setShowMode(ShowMode.PullOut);
        swipeItem.setDragEdge(DragEdge.Right);

        swipeItem.addSwipeListener(new SimpleSwipeListener() {
            @Override
            public void onOpen(ZSwipeItem layout) {
//                LogUtil.i(TAG, "打开:" + position);
            }

            @Override
            public void onClose(ZSwipeItem layout) {
//                LogUtil.i(TAG, "关闭:" + position);
            }

            @Override
            public void onStartOpen(ZSwipeItem layout) {
//                LogUtil.i(TAG, "准备打开:" + position);
            }

            @Override
            public void onStartClose(ZSwipeItem layout) {
//                LogUtil.i(TAG, "准备关闭:" + position);
            }

            @Override
            public void onHandRelease(ZSwipeItem layout, float xvel, float yvel) {
//                LogUtil.i(TAG, "手势释放");
            }

            @Override
            public void onUpdate(ZSwipeItem layout, int leftOffset,
                                 int topOffset) {
//                LogUtil.i(TAG, "位置更新");
            }
        });

        delBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(context, "删除" + position, Toast.LENGTH_SHORT)
                        .show();
                swipeItem.close();
            }
        });

    }

    public void setData(List<VehicleModel> date,List<YWDJPrepareItemModel> ownerShopList) {
        this.data = date;
        this.ownerShopList=ownerShopList;
        notifyDataSetChanged();
    }

    public void addDate(List<VehicleModel> dataList) {
        if(data==null){
            data=new ArrayList<VehicleModel>();
        }
        boolean isUpdate=false;
        for(int i=0;i<dataList.size();i++){
            if(!data.contains(dataList.get(i))){
                data.add(dataList.get(i));
                isUpdate=true;
            }
        }
        if(isUpdate){
            notifyDataSetChanged();
        }
    }
}
